import { BaseSession } from './base'
import { fromAny, StringValue } from '../proto'
import { Modal } from '../modal'
export class GenericDialogSession extends BaseSession {
    private dialog: Modal
    constructor(render: HTMLElement) {
        super('GenericDialog', render)
    }
    onCreate(sessionId: number, futureId: number, ...argv) {
        super.onCreate(sessionId, futureId, ...argv);
        const title = fromAny(argv[0], StringValue)
        const message = fromAny(argv[1], StringValue)
        const okButtonText = fromAny(argv[2], StringValue)
        const cancelButtonText = fromAny(argv[3], StringValue)
        const buttons = []
        if (cancelButtonText)
            buttons.push({
                text: cancelButtonText,
                flag: 'c'
            })
        if (okButtonText)
            buttons.push({
                text: okButtonText,
                flag: 'o'
            })
        const config = {
            title,
            template: message,
            buttons
        }
        this.dialog = new Modal(config)
    }
    onDestroy() {
        this.dialog.close()
        super.onDestroy();
    }
    onReceive(name: String, sessionId: number, futureId: number, ...argv) {
        switch (name) {
            case 'setOnOkButtonClickListener':
                return this.setOnOkButtonClickListener(...argv)
            case 'setOnCancelButtonClickListener':
                return this.setOnCancelButtonClickListener(...argv)
            case 'setOnCancelListener':
                return this.setOnCancelListener(...argv)
        }
        console.log(name)
        super.onReceive(name, sessionId, futureId, ...argv);
    }
    private setOnOkButtonClickListener(...argv) {
        this.dialog.config.buttons.find(i => i.flag == 'o').click = () => {
            this.sendCallback(argv[0])
        }
    }
    private setOnCancelButtonClickListener(...argv) {
        this.dialog.config.buttons.find(i => (i.flag == 'c')).click = () => {
            this.sendCallback(argv[0])
        }
    }
    private setOnCancelListener(...argv) {
        this.dialog.config.onclose = () => {
            this.sendCallback(argv[0])
        }
    }
}